package com.jswlw.controller.admin.biz;

import com.github.pagehelper.Page;
import com.jswlw.controller.BaseController;
import com.jswlw.entity.biz.DistributionRecord;
import com.jswlw.entity.biz.Distributor;
import com.jswlw.service.biz.DistributionRecordService;
import com.jswlw.service.biz.DistributorService;
import com.jswlw.web.manager.SessionAdminManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/admin/biz/distributionRecord")
public class AdminDistributionRecordController extends BaseController {

    @Autowired
    DistributionRecordService service;
    @Autowired
    DistributorService distributorService;

    @RequestMapping("/index")
    public String index(Model model) {
        return "biz/distributionRecord/index";
    }

    @RequestMapping("/totalIndex")
    public String totalIndex(Model model) {
        return "biz/distributionRecord/totalIndex";
    }

    /**
     * 广告列表
     *
     * @param
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestBody DistributionRecord distributionRecord) {
        if (Objects.nonNull(SessionAdminManager.getCurrentUser().getDistributor())) {
            distributionRecord.setDistributorIds(SessionAdminManager.getCurrentUser().getDistributor().getChildrenIds());
        }
        Page<DistributionRecord> page = service.queryPage(distributionRecord);
        return pageResult(page.getResult(), page.getTotal());
    }

    @RequestMapping("/totalList")
    @ResponseBody
    public Object totalList(@RequestBody Distributor filter) {
        if (Objects.nonNull(SessionAdminManager.getCurrentUser().getDistributor())) {
            filter.setDistributorIds(SessionAdminManager.getCurrentUser().getDistributor().getChildrenIds());
        }
        Page<Distributor> page = distributorService.queryPage(filter, filter.getOffset(), filter.getLimit());
        List<Distributor> list = new ArrayList<>();
        page.getResult().forEach(distributor -> {
            DistributionRecord recordFilter = new DistributionRecord();
            recordFilter.setDistributorId(distributor.getId());
            int all = service.totalAmount4Distributor(recordFilter);
            recordFilter.setStatus(0);
            int will = service.totalAmount4Distributor(recordFilter);
            distributor.setAllRecord(all);
            distributor.setWillRecord(will);
            distributor.setRecorded(all - will);
            list.add(distributor);
        });
        return pageResult(list, page.getTotal());
    }

    /**
     * 新增广告
     *
     * @return
     */
    @RequestMapping("/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("biz/distributionRecord/edit");
        return modelAndView;
    }

    /**
     * 保存广告
     *
     * @param distributionRecord
     * @param
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap save(@RequestBody DistributionRecord distributionRecord) {
        try {
            if (distributionRecord.getId() == null) {
                service.add(distributionRecord);
            } else {
                service.update(distributionRecord);
            }
            return this.successResult(null);
        } catch (Exception e) {
            logger.error("保存失败", e);
            return this.failResult("保存失败" + e.getMessage());
        }
    }
}
